//
//  PersonalController.swift
//  XiaoJingMallSwift
//
//  Created by beisheng03 on 2017/7/21.
//  Copyright © 2017年 beisheng. All rights reserved.
//

import UIKit
import SVProgressHUD

class PersonalController: BaseViewController {
    
    var dataModel : PersonalDataModel?
    
//    var tag = 1
    
    var tapIndex = -1
    
    
    
    override func viewWillAppear(_ animated: Bool) {
        super.viewWillAppear(animated)
        
        navigationController?.setNavigationBarHidden(true, animated: true)
        
        
//        if tag == 1 {
//            tag += 1
//            plainTableView.mj_header.beginRefreshing()
//            
//        } else {
        
            loadMeData()
            
//        }
        
    }

    override func viewDidLoad() {
        super.viewDidLoad()
        
        
        automaticallyAdjustsScrollViewInsets = false
        
        plainTableView.contentInset = UIEdgeInsetsMake(0, 0, 49, 0)
        
        plainTableView.register(BaseTableViewCell.self, forCellReuseIdentifier: BaseTableViewCell.identifer())
        
        plainTableView.register(UINib(nibName: StringFromClass(classType: MeTopHeaderCell.self), bundle: nil), forCellReuseIdentifier: MeTopHeaderCell.identifer())
        
        plainTableView.register(UINib(nibName: StringFromClass(classType: MeCommonCell.self), bundle: nil), forCellReuseIdentifier: MeCommonCell.identifer())
        
        plainTableView.register(UINib(nibName: StringFromClass(classType: MeFourImageCell.self), bundle: nil), forCellReuseIdentifier: MeFourImageCell.identifer())
        
        plainTableView.register(UINib(nibName: StringFromClass(classType: MeFiveImageCell.self), bundle: nil), forCellReuseIdentifier: MeFiveImageCell.identifer())
        
        
        //        weak var weakSelf = self
        //
        //        plainTableView.mj_header =  ZMRefreshHeader.init {
        //
        //            weakSelf!.loadMeData()
        //
        //        }
        


        // Do any additional setup after loading the view.
    }
    
    
    override func viewDidAppear(_ animated: Bool) {
        
        super.viewDidAppear(animated)
        
         vipPushTag = false
    }
    
    

    override func didReceiveMemoryWarning() {
        super.didReceiveMemoryWarning()
        // Dispose of any resources that can be recreated.
    }
    
    
 
}


extension PersonalController: MeFiveImageCellDeleage{


    func meFiveImageCellBtnDidTap(str: String) {
        
        
        
        if UserViewModel.shared.userIsLogin() == false {
            
            let log = LoginViewController()
            
            
            present(BaseNavController(rootViewController: log), animated: true, completion: nil)
            
            return
        }

        
        let temp = AllOrderViewController()
        
        if str == "退款/退货" {
            
            temp.type = .ReturnOrderType

        } else {
            
            temp.type = .GoodsOrderType

        }
        

        switch str {
           case "待付款":
            temp.seletIndex = 1
            case "待发货":
                temp.seletIndex = 2

            case "待收货":
                temp.seletIndex = 3

            case "待评价":
                temp.seletIndex = 4

            case "退款/退货":
                temp.seletIndex = 0

        default:
            temp.seletIndex = 0
        }
        
        navigationController?.pushViewController(temp, animated: true)

    }


}


extension PersonalController{

    fileprivate func loadMeData(){
    
        if UserViewModel.shared.userIsLogin() == false {
            
            dataModel = PersonalDataModel(dic: ["favorites_goods" : " " as AnyObject,"foot_prints" : " " as AnyObject,"nickname" : "登录/注册" as AnyObject])
            
            
            plainTableView.reloadData()
            
            return
        }
        
        
            
        SVProgressHUD.show(withStatus: "正在加载数据...")

    
        NetworkTools.shared.sendRequest(method: .GET_METHOD, pathStr: "mobile/index.php?c=member_index&a=index", parameters: nil) {[weak self] (responseObject, error) in
            
            
            if error == nil {
                
                SVProgressHUD.dismiss()
                
                let temp = responseObject?["datas"]?["member_info"] as! [String : AnyObject]
                
                self!.dataModel = PersonalDataModel(dic: temp)
                
                //                self!.plainTableView.mj_header.endRefreshing()
                
                self!.plainTableView.reloadData()
                
            }else{
                
                //                self!.plainTableView.mj_header.endRefreshing()
                
                SVProgressHUD.showError(withStatus: error?.domain)
                
                
            }
            
            
            
            
        }
    
    
    
    }
    
    

}




// MARK:- tableview代理方法
extension PersonalController{
    
    override func numberOfSections(in tableView: UITableView) -> Int {
        
        return 1
        
    }
    
    
    
    override func tableView(_ tableView: UITableView, numberOfRowsInSection section: Int) -> Int {
        
        return 12
        
    }
    
    
    
    override func tableView(_ tableView: UITableView, cellForRowAt indexPath: IndexPath) -> UITableViewCell {
        
        var tableCell : UITableViewCell? = nil
        
        switch indexPath.row {
        case 0:
            let tempCell : MeTopHeaderCell =  tableView.dequeueReusableCell(withIdentifier: MeTopHeaderCell.identifer()) as! MeTopHeaderCell
            tempCell.model = dataModel
            tableCell = tempCell
        case 2:
            let tempCell : MeFiveImageCell =  tableView.dequeueReusableCell(withIdentifier: MeFiveImageCell.identifer()) as! MeFiveImageCell
            tempCell.model = dataModel
            tempCell.delegate = self
            tableCell = tempCell
        case 3:
            let tempCell : BaseTableViewCell =  tableView.dequeueReusableCell(withIdentifier: BaseTableViewCell.identifer()) as! BaseTableViewCell
            tableCell = tempCell
            
        case 9:
            let tempCell : BaseTableViewCell =  tableView.dequeueReusableCell(withIdentifier: BaseTableViewCell.identifer()) as! BaseTableViewCell
            tableCell = tempCell
            
        case 11:
            let tempCell : MeFourImageCell =  tableView.dequeueReusableCell(withIdentifier: MeFourImageCell.identifer()) as! MeFourImageCell
            tempCell.model = dataModel
            
            tableCell = tempCell
            
        default:
            let tempCell : MeCommonCell =  tableView.dequeueReusableCell(withIdentifier: MeCommonCell.identifer()) as! MeCommonCell
            
            tempCell.model = dataModel
            
            tempCell.index = indexPath
            
            tableCell = tempCell
            
            
        }
        
        
        return tableCell!
    }
    
    
    func tableView(_ tableView: UITableView, heightForRowAt indexPath: IndexPath) -> CGFloat {
        
        switch indexPath.row {
        case 0:
            return 196
        case 2:
            return 72
        case 3:
            return 10
        case 9:
            return 10
        case 11:
            return 90
        default:
            return 46
            
        }
        
    }
    
    
    
    
    override func tableView(_ tableView: UITableView, didSelectRowAt indexPath: IndexPath) {
        //        super.tableView(tableView, didSelectRowAt: indexPath)
        
        if indexPath.row == 0 {
            return
        }
        
        switch indexPath.row {
        case 1:
            tapIndex = 1
            performIsLoginJump(aSelector: #selector(test), target: self)
        case 4:
            tapIndex = 4
            
            performIsLoginJump(aSelector: #selector(test), target: self)
        case 5:
            tapIndex = 5
            
            performIsLoginJump(aSelector: #selector(test), target: self)
            
        case 6:
            tapIndex = 6
            
            performIsLoginJump(aSelector: #selector(test), target: self)
            
        case 7:
            tapIndex = 7
            
            performIsLoginJump(aSelector: #selector(test), target: self)
            
        case 8:
            tapIndex = 8
            
            performIsLoginJump(aSelector: #selector(test), target: self)
            
        default:
            
            performIsLoginJump(aSelector: #selector(hehe), target: self)
        }
        
        
    }
    
    
    
}





// MARK:- 跳转方法
extension PersonalController{
    
    func hehe() {
        
    }

    func test() {
        
        
        switch tapIndex {
            case 1:
                
                let temp = AllOrderViewController()
                temp.type = .GoodsOrderType
                temp.seletIndex = 0
                navigationController?.pushViewController(temp, animated: true)

            case 4:
                
                let temp = AllOrderViewController()
                
                temp.type = .OneDollarType
                
                navigationController?.pushViewController(temp, animated: true)


            case 5:
            
                let temp = AllOrderViewController()
                
                temp.type = .GameOrderType
                
                navigationController?.pushViewController(temp, animated: true)


            case 6:
            
                let temp = MyBalanceController()
                
                temp.navigationItem.title = "我的余额"
                
                navigationController?.pushViewController(temp, animated: true)


            case 7:
            
                let temp = MyGoldController()
                navigationController?.pushViewController(temp, animated: true)

            case 8:
                let temp = MyBeanController()
            
                navigationController?.pushViewController(temp, animated: true)

            default:
                
                QLPlusLine()

        }
        
    }








}
